<?php

namespace app\admin\controller\other;

use app\common\controller\Backend;
use fast\Tree;
/**
 * 新闻资讯分类
 *
 * @icon fa fa-circle-o
 */
class FlashCategory extends Backend
{

    /**
     * NewsCategory模型对象
     * @var \app\admin\model\other\FlashCategory
     */
    protected $model = null;

//    public function _initialize()
//    {
//        parent::_initialize();
//        $this->model = new \app\admin\model\news\NewsCategory;
//        $this->view->assign("statusList", $this->model->getStatusList());
//    }

    protected $categorylist=null;
    public function _initialize()
    {



        parent::_initialize();
        $this->model = new \app\admin\model\other\FlashCategory;
        $tree = Tree::instance();
        $tree->init(collection($this->model->order('weigh desc,id desc')->select())->toArray(), 'pid');
        $info=$tree->getTreeList($tree->getTreeArray(0), 'name');
        foreach ($info as $k => $v) {
            $info[$k]['name']=str_replace("&nbsp;", " ", $v['name']);
            $info[$k]['spacer']=str_replace("&nbsp;", " ", $v['spacer']);
        }
        $this->categorylist = $info;
        $categorydata = [0 => ['id' => '0', 'name' => __('None')]];
        foreach ($this->categorylist as $k => $v) {
            $categorydata[$v['id']] = $v;
        }
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("parentList", $categorydata);
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            $list = $this->categorylist;;
            $total = count($list);
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }
    public function edit($ids = null)
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($this->request->isPost()) {
            //$this->token();
            $params = $this->request->post("row/a", [], 'strip_tags');
            if ($params) {

                if ($params['pid'] == $row['id']) {
                    $this->error(__('父组别不能是它的子组别或它自己'));
                }
                if ($params['pid'] != $row['pid']) {
                    $childrenIds = Tree::instance()->init(collection(\app\admin\model\other\FlashCategory::select())->toArray())->getChildrenIds($row['id']);

                    if (in_array($params['pid'], $childrenIds)) {
                        $this->error(__('父组别不能是它的子组别或它自己'));
                    }
                }
                //这里需要针对name做唯一验证
                $result = $row->save($params);
                if ($result === false) {
                    $this->error($row->getError());
                }
                $this->success();
            }
            $this->error();
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

}
